package com.radio.svc.controllers.radio;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.radio.entity.hibernate.ArtistEntity;
import com.radio.entity.hibernate.SongEntity;
import com.radio.svc.common.CommonFile;
import com.radio.svc.controllers.radio.jsonentities.NextSongRequest;
import com.radio.svc.controllers.radio.jsonentities.NextSongResult;
import com.radio.svc.controllers.radio.jsonentities.PickSongRequest;
import com.radio.svc.exceptions.SongNotExistsException;
import com.radio.svc.exceptions.UserNotExistsException;
import com.radio.svc.serviceinterface.IGenreServiceProvider;
import com.radio.svc.serviceinterface.ISongServiceProvider;
import com.radio.svc.serviceinterface.IUserServiceProvider;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * User: farhad
 * Date: 2/15/15
 * Time: 3:25 PM
 * To change this template use File | Settings | File Templates.
 */
public class PreSetSongsController {

    private IUserServiceProvider userServiceProvider;
    private IGenreServiceProvider genreServiceProvider;
    private ISongServiceProvider songServiceProvider;
    private CommonFile commonFile;

    public ISongServiceProvider getSongServiceProvider() {
        return songServiceProvider;
    }

    public void setSongServiceProvider(ISongServiceProvider songServiceProvider) {
        this.songServiceProvider = songServiceProvider;
    }

    public IUserServiceProvider getUserServiceProvider() {
        return userServiceProvider;
    }

    public void setUserServiceProvider(IUserServiceProvider userServiceProvider) {
        this.userServiceProvider = userServiceProvider;
    }



    public IGenreServiceProvider getGenreServiceProvider() {
        return genreServiceProvider;
    }

    public void setGenreServiceProvider(IGenreServiceProvider genreServiceProvider) {
        this.genreServiceProvider = genreServiceProvider;
    }

    @RequestMapping(value = "/perSetStationSong.ajax", method = RequestMethod.POST)
    @ResponseBody
    public String nextSong(@RequestBody String json) throws IOException, UserNotExistsException {

        ObjectMapper objectMapper = new ObjectMapper();
        PickSongRequest pickSongRequest = objectMapper.readValue(json, PickSongRequest.class);

        String userName = null;
        List<SongEntity> result = null;
        String resultSongArtWork = null;
        String resultSongTitle = null;
        String resultSongArtist = null;
        String resultArtistInfo = null;
        Long resultSongID = null;
        boolean resultLikeStatus = false;
        boolean resultDislikeStatus = false;
        int dislikeCount = 0;
        int likeCount = 0;
        String resultSongPath = null;

        try {
            //songServiceProvider.getTopSongs();
            result = genreServiceProvider.listSongsByGenreName(pickSongRequest.getGenre().replaceAll("%20", " "));
            userName = pickSongRequest.getUsername();
            Random rand = new Random();
            int randomNumber = rand.nextInt(result.size());
            SongEntity nextSong = result.get(randomNumber);
            resultSongArtWork = nextSong.getArtworkPath();
            resultSongTitle = nextSong.getName();
            resultSongID = nextSong.getSongID();
            resultSongPath = nextSong.getMp3Path();
            ArtistEntity art = new ArtistEntity();
            Iterator i = nextSong.getArtists().iterator();


            while (i.hasNext()) {
                ArtistEntity artistEntity =(ArtistEntity) i.next();
                resultArtistInfo += artistEntity.getArtistInfo() + "\n";
                resultSongArtist += artistEntity.getArtistName()+ " ";
                }

        } catch (Exception e) {
            e.printStackTrace();
        }

        //--------------------------- Like
        //boolean likeStatus = false;
        // List<SongEntity> liked = new ArrayList<SongEntity>();
        // try {
        List<SongEntity> liked = songServiceProvider.getUsersLikedSong(userName);

        Iterator b = liked.iterator();


        while (b.hasNext()) {
            SongEntity songEntity = (SongEntity) b.next();

            if (resultSongID.equals(songEntity.getSongID())) {

                resultLikeStatus = true;

            }
        }
        try {
            likeCount = songServiceProvider.getNumberOfLikes(resultSongID);
            dislikeCount = songServiceProvider.getNumberOfDislikes(resultSongID);
        } catch (SongNotExistsException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            System.out.println("Song not exist!");
        }

        List<SongEntity> disliked = songServiceProvider.getUsersDislikedSong(userName);
        //dislikeCount = disliked.size();
        Iterator c = disliked.iterator();


        while (c.hasNext()) {
            SongEntity songEntity = (SongEntity) c.next();
            if (resultSongID.equals(songEntity.getSongID())) {

                resultDislikeStatus = true;

            }
        }
       /* } catch (UserNotExistsException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }*/


        NextSongResult nextSongResult = new NextSongResult();
        nextSongResult.setUrl("http://localhost:8080/songs/" + resultSongPath);
        nextSongResult.setArtWorkUrl("http://localhost:8080/song-artwork/" + resultSongArtWork);
        nextSongResult.setTitle(resultSongTitle);
        nextSongResult.setArtist(resultSongArtist);
        nextSongResult.setArtistInfo(resultArtistInfo);
        nextSongResult.setSongID(resultSongID);
        nextSongResult.setLikeStatus(resultLikeStatus);
        nextSongResult.setDislikeStatus(resultDislikeStatus);
        nextSongResult.setDislikeCount(dislikeCount);
        nextSongResult.setLikeCount(likeCount);


        return toJson(nextSongResult);

    }

    private String toJson(NextSongResult nextSongResult) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            String value = mapper.writeValueAsString(nextSongResult);
            return value;
        } catch (Exception e) {
            return null;
        }
    }

}